﻿using FluentValidation;
using Nuclear.Models.DtoModels;

namespace Nuclear.Validations.DtoValidations;

public class MerchantLoginRequestDtoValidator : AbstractValidator<MerchantLoginRequestDto>
{
    private const int MinUserNameLength = 3,
        MinPasswordLength = 6;

    public MerchantLoginRequestDtoValidator()
    {
        RuleFor(x => x.SystemId).MinimumLength(2)
            .WithErrorCode("System id must be at least 2 letters")
            .WithMessage("系统ID至少包含两个字符");
        RuleFor(x => x.UserName).
            Must(x =>
            {
                var trimmedUserName = x.Trim();
                return trimmedUserName.Length >= MinUserNameLength &&
                       trimmedUserName.All(char.IsAsciiLetterOrDigit);
            })
            .WithErrorCode($"User name must be at least {MinUserNameLength} characters long and must all be letters or digits")
            .WithMessage($"用户名至少{MinUserNameLength}位, 只能包含字母和数字");
        RuleFor(x => x.Password)
            .Must(x =>
            {
                var trimmedPassword = x.Trim();
                return trimmedPassword.Length >= MinPasswordLength &&
                       trimmedPassword.All(c => char.IsAsciiLetterOrDigit(c) ||
                                                char.IsBetween(c, '!', '/') ||
                                                char.IsBetween(c, ':', '@') ||
                                                char.IsBetween(c, '[', '`') ||
                                                char.IsBetween(c, '{', '~')
                                                );
            })
            .WithErrorCode($"Password must be at least {MinPasswordLength} characters long and all be letters or digits or special characters")
            .WithMessage($"密码至少{MinPasswordLength}位, 只能包含字母、数字或者特殊字符");
    }
}